package com.whjfxx.terminal.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whjfxx.terminal.biz.mapper.UserTokenMapper;
import com.whjfxx.terminal.biz.service.UserTokenService;
import com.whjfxx.terminal.entity.UserToken;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Objects;

/**
 * Created by b.c on 2022/4/12 14:55
 */
@Service
public class UserTokenServiceImpl extends ServiceImpl<UserTokenMapper, UserToken> implements UserTokenService {


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(Long userId, String token) {
        Objects.requireNonNull(userId);
        Objects.requireNonNull(token);

        LambdaQueryWrapper<UserToken> wrapper = new LambdaQueryWrapper();
        wrapper.eq(UserToken::getUserId, userId);
        UserToken userToken = this.getOne(wrapper);
        if (Objects.nonNull(userToken)) {
            userToken.setToken(token);
            userToken.setUpdateTime(new Date());
            this.updateById(userToken);
        } else {
            userToken = new UserToken();
            userToken.setUserId(userId);
            userToken.setToken(token);
            userToken.setCreateTime(new Date());
            userToken.setUpdateTime(new Date());
            this.save(userToken);
        }
    }

    @Override
    public void valid(Long userId, String token) {

    }
}
